package com.dux.commons.network;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.util.Iterator;


/**
 * 这个调度器只用于接受连接
 * @author 杜祥
 * @create 2013-6-4
 * @see com.game.commons.network.Dispatcher
 */
public class AcceptDispatcherImpl extends Dispatcher
{
	
	/**
	 * 构造一个新的调度器，并设置调度器的名称
	 * @param name
	 * @throws IOException
	 */
	public AcceptDispatcherImpl(String name)throws IOException
	{
		super(name, null);
	}

	
	@Override
	void closeConnection(MMOConnection connection) 
	{
		throw new UnsupportedOperationException("这个方法不应该被调用。");
	}
	

	/**
	 * 调度方法
	 * @see com.game.commons.network.Dispatcher#dispatch()
	 */
	@Override
	void dispatch() throws IOException 
	{
		if(this.selector.select() != 0)
		{
			Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();
			
			while(selectedKeys.hasNext())
			{
				SelectionKey key = selectedKeys.next();
				selectedKeys.remove();
				
				if(key.isValid())
					accept(key);
			}
		}
	}

}
